package com.aidex.admin.controller.system;

import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.domain.entity.SysMenu;
import com.aidex.common.enums.BusinessType;
import com.aidex.system.service.SysMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单信息
 */
@RestController
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController<SysMenuService> {

    /**
     * 获取菜单列表
     */
    @PreAuthorize("@ss.hasPermi('system:menu:list')")
    @GetMapping("/list")
    public R list() {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", "0");
        wrapper.eq("del_flag", "0");
        List<SysMenu> menus = service.selectList(wrapper);
        return R.data(menus);
    }

    /**
     * 根据菜单编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:menu:query')")
    @GetMapping(value = "/{id}")
    public R detail(@NotBlank @PathVariable String id) {
        return R.data(service.selectById(id));
    }

    /**
     * 获取当前父节点下最大编号
     *
     * @param parentId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:menu:query')")
    @Log(title = "菜单管理", businessType = BusinessType.SELECT)
    @GetMapping("/findMaxSort/{parentId}")
    public R findMaxSort(@PathVariable("parentId") String parentId) {
        SysMenu entity = new SysMenu();
        entity.setParentId(parentId);
        return R.data(service.findMaxSort(entity));
    }

    @GetMapping("/treeselect/{level}/{id}")
    public R treeselect(SysMenu menu, @NotBlank @PathVariable("level") int level, @PathVariable("id") String id) {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", "0");
        wrapper.eq("status", "0");
        wrapper.eq("del_flag", "0");
        List<SysMenu> menuList = service.selectList(wrapper);
        return R.data(service.buildMenusTree(menuList));
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    public R roleMenuTreeselect(@PathVariable("roleId") String roleId) {
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("checkedKeys", service.selectMenuIdsByRoleId(roleId));
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", "0");
        wrapper.eq("status", "0");
        wrapper.eq("del_flag", "0");
        List<SysMenu> menuList = service.selectList(wrapper);
        ajax.put("menus", service.buildMenusTree(menuList));
        return R.data(ajax);
    }

    /**
     * 新增菜单
     */
    @PreAuthorize("@ss.hasPermi('system:menu:add')")
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R save(@Validated @RequestBody SysMenu entity) {
        service.insert(entity);
        return R.data(entity);
    }


    /**
     * 删除菜单
     */
    @PreAuthorize("@ss.hasPermi('system:menu:remove')")
    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{menuId}")
    public R remove(@PathVariable("menuId") String menuId) {
        if (service.hasChildByMenuId(menuId)) {
            return R.fail("存在子菜单,不允许删除");
        }
        if (service.checkMenuExistRole(menuId)) {
            return R.fail("菜单已分配,不允许删除");
        }
        return toAjax(service.deleteById(menuId));
    }

    /**
     * 校验菜单名称是否存在
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典类型", businessType = BusinessType.CHECK)
    @GetMapping("/checkMenuNameUnique")
    public R checkMenuNameUnique(SysMenu entity) {
        Map<String, String> checkMap = new HashMap<>();
        try {
            service.checkMenuNameUnique(entity);
            checkMap.put("code", "1");
        } catch (Exception e) {
            checkMap.put("code", "2");
        }
        return R.data(checkMap);
    }

    /**
     * 校验路由地址是否存在
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典类型", businessType = BusinessType.CHECK)
    @GetMapping("/checkMenuCodeUnique")
    public R checkMenuCodeUnique(SysMenu entity) {
        Map<String, String> checkMap = new HashMap<>();
        try {
            service.checkMenuCodeUnique(entity);
            checkMap.put("code", "1");
        } catch (Exception e) {
            checkMap.put("code", "2");
        }
        return R.data(checkMap);
    }
}